package dev_ops.tools.kafka;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

/**
 * @see <a href=
 *      "https://kafka.apache.org/090/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html">090_KafkaConsumer.html</a>
 */
public class KafkaConsumer_WR {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,sss");
    public static void main(String[] args) {
        Properties props = newConsumer_envTest();
        KafkaConsumer<String, String> consumer = null;
        try {
            consumer = new KafkaConsumer<String, String>(props);
            consumer.subscribe(Arrays.asList(
                     //"bizType-media",
                     //"zhangxin-server",
                     // "bizType-cm"
                     // "bizType-user"
                    // "ympns-push"
                    // "bizType-pot"
                    // "bizType-user"
                    "bizType-friend"
                     ));
            //consumer.
            while (true) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ConsumerRecords<String, String> records = consumer.poll(1000);
                for (ConsumerRecord<String, String> record : records) {
                    System.out.printf("offset=%d, key=%s, ts=%s, value=%s\n", record.offset(), record.key(),
                            new Date(record.timestamp()), record.value());
                }
            }
        } finally {
            if (null != consumer) {
                consumer.close();
            }
        }
    }

    public static Properties newConsumer_envTest() {
        Properties props = newCommonConsumer();
        //props.put("bootstrap.servers", "10.136.24.76:9091");
        // props.put("bootstrap.servers", "10.135.28.4:9091");
        props.put("bootstrap.servers", "10.135.28.6:9091");
        //props.put("bootstrap.servers", "10.241.46.41:9091");
        //props.put("group.id", "groupx-zhangxin-server222");
        // props.put("group.id", "ympns-young222");
        // props.put("group.id", "rcc_pot");
        //props.put("group.id", "friend_uic");
        props.put("group.id", "recommend-task-1");
        return props;
    }
    
    public static Properties newCommonConsumer() {
        Properties props = new Properties();
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", "1000");
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer", KafkaUtils.String_deser);
        props.put("value.deserializer", KafkaUtils.String_deser);
        props.put("max.poll.records", "3");
        return props;
    }
}
